package com.sunday.fangdaifu.widgets;


import android.os.CountDownTimer;
import android.widget.TextView;

import com.sunday.fangdaifu.utils.DateUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * Created by 刘涛 on 2017/7/21.
 */

public class RecordTimer extends CountDownTimer {

    private TextView getCodeBtn;
    private FinishCallBack onFinishCallBackListener;
    private long totalMills;
    public RecordTimer(long millisInFuture, long countDownInterval, TextView getCodeBtn) {
        super(millisInFuture, countDownInterval);//参数依次为总时长,和计时的时间间隔
        this.getCodeBtn = getCodeBtn;
        this.totalMills=millisInFuture;
    }

    @Override
    public void onFinish() {//计时完毕时触发
        //getCodeBtn.setText("重新发送");
       // getCodeBtn.setClickable(true);
        getCodeBtn.setTag(1);
        if(onFinishCallBackListener!=null){
            onFinishCallBackListener.onFinish();
        }
    }


    @Override
    public void onTick(long millisUntilFinished) {//计时过程显示
        getCodeBtn.setClickable(false);
        String strTime="";
        try {
           strTime=DateUtil.longToString(totalMills-millisUntilFinished,"mm:ss");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        getCodeBtn.setText(strTime);
    }

    public interface FinishCallBack{
        void onFinish();
    }

    public void setOnFinishCallBackListener(FinishCallBack onFinishCallBackListener) {
        this.onFinishCallBackListener = onFinishCallBackListener;
    }
}
